1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.testing;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  import static junit.framework.Assert.assertEquals;
21  import static junit.framework.Assert.assertTrue;
22  
23  import com.google.common.annotations.Beta;
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.base.Equivalence;
26  import com.google.common.collect.ImmutableList;
27  import com.google.common.collect.Lists;
28  import com.google.common.testing.RelationshipTester.ItemReporter;
29  
30  import java.util.List;
31  
32  /**
33   * Tester for {@link Equivalence} relationships between groups of objects.
34   *
35   * <p>
36   * To use, create a new {@link EquivalenceTester} and add equivalence groups
37   * where each group contains objects that are supposed to be equal to each
38   * other. Objects of different groups are expected to be unequal. For example:
39   *
40   * <pre>
41   * {@code
42   * EquivalenceTester.of(someStringEquivalence)
43   *     .addEquivalenceGroup("hello", "h" + "ello")
44   *     .addEquivalenceGroup("world", "wor" + "ld")
45   *     .test();
46   * }
47   * </pre>
48   *
49   * <p>
50   * Note that testing {@link Object#equals(Object)} is more simply done using
51   * the {@link EqualsTester}. It includes an extra test against an instance of an
52   * arbitrary class without having to explicitly add another equivalence group.
53   *
54   * @author Gregory Kick
55   * @since 10.0
56   *
57   * TODO(gak): turn this into a test suite so that each test can fail
58   * independently
59   */
60  @Beta
61  @GwtCompatible public final class EquivalenceTester<T> {
62    private static final int REPETITIONS = 3;
63  
64    private final Equivalence<? super T> equivalence;
65    private final RelationshipTester<T> delegate;
66    private final List<T> items = Lists.newArrayList();
67  
68    private EquivalenceTester(Equivalence<? super T> equivalence) {
69      this.equivalence = checkNotNull(equivalence);
70      this.delegate = new RelationshipTester<T>(
71          equivalence, "equivalent", "hash", new ItemReporter());
72    }
73  
74    public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
75      return new EquivalenceTester<T>(equivalence);
76    }
77  
78    /**
79     * Adds a group of objects that are supposed to be equivalent to each other
80     * and not equivalent to objects in any other equivalence group added to this
81     * tester.
82     */
83    public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
84      addEquivalenceGroup(Lists.asList(first, rest));
85      return this;
86    }
87  
88    public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
89      delegate.addRelatedGroup(group);
90      items.addAll(ImmutableList.copyOf(group));
91      return this;
92    }
93  
94    /** Run tests on equivalence methods, throwing a failure on an invalid test */
95    public EquivalenceTester<T> test() {
96      for (int run = 0; run < REPETITIONS; run++) {
97        testItems();
98        delegate.test();
99      }
100     return this;
101   }
102 
103   private void testItems() {
104     for (T item : items) {
105       /*
106        * TODO(cpovirk): consider no longer running these equivalent() tests on every Equivalence,
107        * since the Equivalence base type now implements this logic itself
108        */
109       assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));
110       assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));
111       assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));
112       assertEquals("the hash of " + item + " must be consistent", equivalence.hash(item),
113           equivalence.hash(item));
114     }
115   }
116 }